﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContinuationSource_When_Completed.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the ContinuationSource_When_Completed type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Testing.Threading;

    [TestClass]
    public class ContinuationSource_When_Completed
    {
        public class TestContinuationSource : ContinuationSource
        {
            public void SetCompleted()
            {
                Completed();
            }
        }

        [TestMethod]
        public void If_Not_Already_Completed_Should_Run_Continuations()
        {
            var result = TestStrings.Failure;

            var continuationSource = new TestContinuationSource();
            continuationSource.ContinueWith(() => result = TestStrings.Success);
            continuationSource.SetCompleted();

            Assert.AreEqual(TestStrings.Success, result);
        }

        [TestMethod]
        public void If_Already_Completed_Should_Not_Run_Continuations()
        {
            var continuationCount = 0;

            var continuationSource = new TestContinuationSource();
            continuationSource.ContinueWith(() => continuationCount++);
            continuationSource.SetCompleted();
            continuationSource.SetCompleted();

            Assert.AreEqual(1, continuationCount);
        }


        [TestMethod]
        [HostType("CHESS")]
        public void If_Invoked_Multiple_Times_On_Different_Threads_Should_Only_Run_Continuations_Once()
        {
            var count = 0;
            var threadDone = false;

            var continuationSource = new TestContinuationSource();
            continuationSource.ContinueWith(() => count++);

            // queue two threads for set completed
            ThreadPool.QueueUserWorkItem(s =>
               {
                   continuationSource.SetCompleted();
                   threadDone = true;
               });
            continuationSource.SetCompleted();

            // at this point we know the Test Threads set complete hase been called, wait for ThreadPool to complete.
            AsyncUtility.SleepUntil(() => threadDone);

            Assert.AreEqual(1, count);
        }

        [TestMethod]
        [HostType("CHESS")]
        public void If_Adding_New_Continuations_From_Different_Threads_Should_Run_Continuations()
        {
            var threadDone = false;

            var continuationSource = new TestContinuationSource();

            ThreadPool.QueueUserWorkItem(p =>
               continuationSource.ContinueWith(() => threadDone = true));

            continuationSource.SetCompleted();

            // at this point we know the Test Threads set complete hase been called, wait for ThreadPools to complete.
            AsyncUtility.SleepUntil(() => threadDone);
        }
    }
}
